# -*- coding: utf-8 -*-
my_collection = {"a": 1, "b": 2, "c": 3}
way_1 = my_collection["b"]
way_2 = my_collection.__getitem__("b")
print(way_1) # 2
print(way_2) # 2

import collections

# 快速创建一个类
Card = collections.namedtuple('Card', ['rank', 'suit'])

class FrenchDeck:
    # 纸牌的大小,从A到K
    ranks = [str(n) for n in range(2, 11)] + list('JQKA')
    # 纸牌的花色
    suits = 'spades diamonds clubs hearts'.split()

    def __init__(self):
        self._cards = [Card(rank, suit) for suit in self.suits
                                        for rank in self.ranks]

    def __len__(self):
        return len(self._cards)

    def __getitem__(self, position):
        return self._cards[position]

deck = FrenchDeck()
# 查看总牌数
print(len(deck)) # 共52张
# 取出第一张牌
print(deck[0])
# 取出最后一张牌
print(deck[-1])
# 随机取一张牌
from random import choice
print(choice(deck))
# 查看前三张牌
print(deck[:3])
# 只看J牌,J是每套同花色的第10张,每隔13张取牌,取四次就可以把J都取出来
print(deck[9::13])
# 因为实现了__getitem__方法,因此deck可迭代
for item in deck:
    print(f"{item.suit}{item.rank}")
# 亦可反向迭代
for item in reversed(deck): # doctest: +ELLIPSIS
    print(f"{item.suit}{item.rank}")
# 将纸牌进行升序排序
suit_values = dict(spades=3, hearts=2, diamonds=1, clubs=0)
def spades_high(card):
    rank_value = FrenchDeck.ranks.index(card.rank)
    return rank_value * len(suit_values) + suit_values[card.suit]
for item in sorted(deck, key=spades_high):
    print(item)
